package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import entity.ButtonEnum;
import qw.easyFrame.view.EasyTextArea;
import logic.Controller;
import qw.easyFrame.enums.SelectionMode;
import qw.easyFrame.view.EasyFileChooser;
import qw.easyFrame.view.EasyRightClickMenu;
import static qw.easyFrame.view.util.SwingUtil.defaultJButton;
import static qw.easyFrame.view.util.SwingUtil.defaultJTextField;
import static qw.easyFrame.view.util.SwingUtil.defaultJPanel;
import static qw.easyFrame.view.util.SwingUtil.setJFrame;

/**
 * 文件批量重命名.
 * @author AL
 * 
 */
public class Door {
 
	private JFrame window;
	private JButton choose, rename, classify, removal;
	private JTextField input;
	private volatile EasyTextArea area;
	private JPanel panel;
	private JScrollPane scroll;
	private volatile JScrollBar bar;// 滚动条
	private Font bigFont, smallFont;
	private EasyFileChooser chooser;

	/**
	 * 用于接收用户选择的文件夹.
	 */
	public static File[] dirs;
	
	/**
	 * 用于标记程序当前是否正在运行，避免用户重复点击.
	 */
	public static volatile boolean running = false;

	private Door() {
		// 初始化变量
		bigFont = new Font("Microsoft yahei", Font.PLAIN, 20);
		smallFont = new Font("Microsoft yahei", Font.PLAIN, 13);
		window = new JFrame("批量重命名工具");
		choose = defaultJButton("选择路径", smallFont);
		rename =  defaultJButton("重命名", smallFont);
		classify =  defaultJButton("归档", smallFont);
		removal =  defaultJButton("去重", smallFont);
		input = defaultJTextField("请输入要插入的关键字", smallFont);
		area = new EasyTextArea(bigFont, Color.GRAY);
		panel = defaultJPanel(1, 4);
		scroll = new JScrollPane(area);
		bar = scroll.getVerticalScrollBar();
		chooser = new EasyFileChooser(area, SelectionMode.DIRS_ONLY);

		// 设置窗口元素样式
		input.setForeground(Color.RED);

		// 设置布局
		Container cp = window.getContentPane();

		// 按照布局添加元素
		panel.add(choose);
		panel.add(rename);
		panel.add(classify);
		panel.add(removal);
		cp.add(panel, BorderLayout.NORTH);
		cp.add(input, BorderLayout.SOUTH);
		cp.add(scroll, BorderLayout.CENTER);

		// 监听鼠标右键
		area.addMouseListener(new EasyRightClickMenu(area, smallFont));

		// 绑定鼠标单击事件
		choose.addActionListener(e -> dirs = chooser.choose());
		rename.addActionListener(e -> Controller.action(input, area, bar, ButtonEnum.Re_Name));
		classify.addActionListener(e -> Controller.action(input, area, bar, ButtonEnum.Files_Classify));
		removal.addActionListener(e -> Controller.action(input, area, bar, ButtonEnum.Duplicate_Removal));

		// 设置窗口属性
		setJFrame(window, 500, 300);
	}
	
	public static void main(String[] args) {
		new Door();
	}
	
}